﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBenhVien
{
    public partial class frmDanhSachBenh : Form
    {
        public DBOBenhNhan ret;
        public frmDanhSachBenh()
        {
            InitializeComponent();
            LoadData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmThongTinBenhNhan t = new frmThongTinBenhNhan();
            if (t.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            DataTable dt = DAOBenhNhan.LoadAll();
            dg.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            DataTable dt = new DataTable();
            if (txtHoten.Text.Length > 0)
            {
                if (txtID.Text.Length > 0)
                {
                    dt = DAOBenhNhan.Search(txtHoten.Text, int.Parse(txtID.Text));
                }
                else
                {
                    dt = DAOBenhNhan.Search(txtHoten.Text);
                }
            }
            else if (txtID.Text.Length > 0)
            {
                try
                {
                    dt = DAOBenhNhan.Search(int.Parse(txtID.Text));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Mã phải là 1 số");
                }
            }
            else
            {
                dt = DAOBenhNhan.LoadAll();
            }
            dg.DataSource = dt;
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Search();
            }
        }

        private void txtHoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Search();
            }
        }

        private void dg_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ret = new DBOBenhNhan();
            ret.id = (int)dg.CurrentRow.Cells["id"].Value;
            ret.hoten = dg.CurrentRow.Cells["hoten"].Value.ToString();
            ret.ngaysinh = DateTime.Parse(dg.CurrentRow.Cells["ngaysinh"].Value.ToString());
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
